package com.sanji.sjmallmodule.phoneshell;

import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.sanji.sjmallapp.BaseActivity;
import com.sanji.sjmallapp.MallApplication;
import com.sanji.sjmallapp.R;
import com.sanji.sjmallmodule.order.MyFragmentPagerAdapter;
import com.sanji.sjmallmodule.order.MyOrderActivity;
import com.sanji.sjmallmodule.order.MyOrderFragment;
import com.sanji.sjmallutils.SharePreferenceUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 定制订单
 * Created by Administrator on 2017/9/16.
 */

public class ShellOrderActivity extends FragmentActivity implements View.OnClickListener,RadioGroup.OnCheckedChangeListener{

    @BindView(R.id.common_back_img)ImageView backImg;
    @BindView(R.id.common_left_tv)TextView titleTv;
    @BindView(R.id.shell_order_all_btn)RadioButton mOrderAllBtn;
    @BindView(R.id.shell_order_redo_btn)RadioButton mOrderRedoBtn;
    @BindView(R.id.shell_order_producing_btn)RadioButton mOrderProducingBtn;
    @BindView(R.id.shell_order_shipped_btn)RadioButton mOrderShippedBtn;
    @BindView(R.id.shell_order_finished_btn)RadioButton mOrderFinishedBtn;
    @BindView(R.id.shell_order_title_group)RadioGroup mOrderTitleGroup;
    @BindView(R.id.shell_order_point_line)ImageView mOrderPointLine;
    @BindView(R.id.shell_order_viewpager)ViewPager mOrderViewpager;

    private int offset = 0;//动画图片偏移量
    private int currIndex = 0;//当前页卡编号
    private int bmpW;//动画图片宽度
    private SharePreferenceUtil myShare;

    private List<ShellOrderFragment> fragmentList;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shell_order);
        ButterKnife.bind(this);
        initViews();
        initImageView();
        initViewPager();
    }

    private void initViews() {
        backImg.setOnClickListener(this);
        titleTv.setText("定制订单");
        mOrderTitleGroup.setOnCheckedChangeListener(this);
        myShare = MallApplication.myShare;
    }

    private void initImageView() {
        //图片宽度
        bmpW = BitmapFactory.decodeResource(getResources(), R.mipmap.line_tab_bom).getWidth();
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        int screenW = dm.widthPixels;//获取屏幕宽度
        offset = (screenW / 5 - bmpW) / 2;//计算偏移量
        Matrix matrix = new Matrix();
        matrix.postTranslate(offset, 0);
        mOrderPointLine.setImageMatrix(matrix);
    }

    private void initViewPager() {
        fragmentList = new ArrayList<>();
        ShellOrderFragment allOrder = new ShellOrderFragment();
        Bundle allOrderBundle = new Bundle();
        allOrderBundle.putInt("pages", 0);
        allOrderBundle.putString("status", "");
        allOrder.setArguments(allOrderBundle);
        //重做
        ShellOrderFragment redoOrder = new ShellOrderFragment();
        Bundle unPaidOrderBundle = new Bundle();
        unPaidOrderBundle.putInt("pages", 1);
        unPaidOrderBundle.putString("status", "重做");
        redoOrder.setArguments(unPaidOrderBundle);
        //生产中
        ShellOrderFragment producingOrder = new ShellOrderFragment();
        Bundle unShipedOrderBundle = new Bundle();
        unShipedOrderBundle.putInt("pages", 2);
        unShipedOrderBundle.putString("status", "处理中");
        producingOrder.setArguments(unShipedOrderBundle);
        //已发货
        ShellOrderFragment shippedOrder = new ShellOrderFragment();
        Bundle paidOrderBundle = new Bundle();
        paidOrderBundle.putInt("pages", 3);
        paidOrderBundle.putString("status", "已发货");
        shippedOrder.setArguments(paidOrderBundle);
        //已完成
        ShellOrderFragment finishedOrder = new ShellOrderFragment();
        Bundle finishBundle = new Bundle();
        finishBundle.putInt("pages", 4);
        finishBundle.putString("status", "制作完成");
        finishedOrder.setArguments(finishBundle);


        fragmentList.add(allOrder);
        fragmentList.add(redoOrder);
        fragmentList.add(producingOrder);
        fragmentList.add(shippedOrder);
        fragmentList.add(finishedOrder);
//        switch (currIndex) {
//            case 0:
//                allOrderBundle.putInt("pages", 0);
//                allOrder.setArguments(allOrderBundle);
//                break;
//            case 1:
//                unPaidOrderBundle.putInt("pages", 1);
//                unPaidOrder.setArguments(unPaidOrderBundle);
//                break;
//            case 2:
//                unShipedOrderBundle.putInt("pages", 2);
//                unShipedOrder.setArguments(unShipedOrderBundle);
//                break;
//            case 3:
//                paidOrderBundle.putInt("pages", 3);
//                paidOrder.setArguments(paidOrderBundle);
//                break;
//        }
        //设置缓存页面个数
        mOrderViewpager.setOffscreenPageLimit(fragmentList.size());
        mOrderViewpager.setAdapter(new ShellFragmentPagerAdapter(getSupportFragmentManager(), fragmentList));
        mOrderViewpager.setOnPageChangeListener(new MyOnPageChangeListener());
        mOrderViewpager.setCurrentItem(0);

    }





    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.common_back_img:
                finish();
                break;
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
        switch (checkedId) {
            case R.id.shell_order_all_btn:
                mOrderViewpager.setCurrentItem(0);
                break;
            case R.id.shell_order_redo_btn:
                mOrderViewpager.setCurrentItem(1);
                break;
            case R.id.shell_order_producing_btn:
                mOrderViewpager.setCurrentItem(2);
                break;
            case R.id.shell_order_shipped_btn:
                mOrderViewpager.setCurrentItem(3);
                break;
            case R.id.shell_order_finished_btn:
                mOrderViewpager.setCurrentItem(4);
                break;
        }
    }




    public class MyOnPageChangeListener implements ViewPager.OnPageChangeListener {

        private int one = offset * 2 + bmpW;//两个相邻页面的偏移量

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            Animation animation = new TranslateAnimation(currIndex * one, position * one, 0, 0);//平移动画
            currIndex = position;
            animation.setFillAfter(true);//动画终止时停留在最后一帧，不然会回到没有执行前的状态
            animation.setDuration(200);//动画持续时间0.2秒
            mOrderPointLine.startAnimation(animation);//是用ImageView来显示动画的
//            int i = currIndex + 1;
//            Toast.makeText(MyOrderActivity.this, "您选择了第"+i+"个页卡", Toast.LENGTH_SHORT).show();
            switch (position) {
                case 0:
                    mOrderAllBtn.setChecked(true);
                    break;
                case 1:
                    mOrderRedoBtn.setChecked(true);
                    break;
                case 2:
                    mOrderProducingBtn.setChecked(true);
                    break;
                case 3:
                    mOrderShippedBtn.setChecked(true);
                    break;
                case 4:
                    mOrderFinishedBtn.setChecked(true);
                    break;
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    }
}
